
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.UI;
using WooEngine.Extensions;
//using UnityEngine.EventSystems;
#endif

#if UNITY_EDITOR
namespace WooEngine.Common
{
    [CustomEditor(typeof(UIProgressELement), true)]
    [CanEditMultipleObjects]
    public class UIProgressELementInspector : ScrollbarEditor
    {
        SerializedProperty m_FillArea;

        protected override void OnEnable()
        {
            base.OnEnable();

            m_FillArea = serializedObject.FindProperty("m_FillArea");
        }


        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            EditorGUILayout.PropertyField(m_FillArea);

        }
    }
}
#endif


namespace WooEngine.Common
{
    public class UIProgressELement : Scrollbar
    {
        [SerializeField]
        private Image m_FillArea;


        protected override void DoStateTransition(SelectionState state, bool instant)
        {
            base.DoStateTransition(state, instant);
            if(this.m_FillArea != null)
               this.m_FillArea.fillAmount = this.value;
        }

        public void SetValue(float v)
        {
            this.value = v;
            if (this.m_FillArea != null)
                this.m_FillArea.fillAmount = this.value;
        }

        public override void OnDrag(PointerEventData eventData)
        {
            base.OnDrag(eventData);
            if (this.m_FillArea != null)
                this.m_FillArea.fillAmount = this.value;
        }

        public void SetFillImg(Sprite s)
        {
            this.m_FillArea.sprite = s;
            if (s != null) this.m_FillArea.color = Color.white;
        }




#if UNITY_EDITOR
        [MenuItem("GameObject/UI/UIProgressELement", false, 3002)]
        private static void OnCreateUIComponent()
        {
            RectTransform rect_view = new GameObject("UIProgressELement").GetOrAddComponent<RectTransform>();
            rect_view.gameObject.layer = LayerMask.NameToLayer("UI");
#if UNITY_2021_2_OR_NEWER
            if (UnityEditor.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage() != null)
#else
            if (UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage() != null)
#endif
            {
                if (Selection.activeGameObject != null)
                    rect_view.SetParent(Selection.activeGameObject.transform, false);
            }
            else
            {
                var canvas = FindObjectOfType<Canvas>();
                if (canvas == null)
                {
                    canvas = new GameObject("Canvas").AddComponent<Canvas>();
                    canvas.renderMode = RenderMode.ScreenSpaceOverlay;

                    canvas.GetOrAddComponent<CanvasScaler>();
                    canvas.GetOrAddComponent<GraphicRaycaster>();
                }

                var eventsystem = FindObjectOfType<EventSystem>();
                if (eventsystem == null)
                {
                    eventsystem = new GameObject("EventSystem").AddComponent<EventSystem>();
                    eventsystem.GetOrAddComponent<StandaloneInputModule>();
                }

                rect_view.SetParent(canvas.transform, false);
            }

            GameObject go_view = rect_view.gameObject;
            go_view.AddComponent<CanvasRenderer>();
            go_view.AddComponent<Image>();

            GameObject go_FillArea = new GameObject("FillArea");
            RectTransform rect_FillArea = go_FillArea.GetOrAddComponent<RectTransform>();
            rect_FillArea.gameObject.AddComponent<CanvasRenderer>();
            rect_FillArea.gameObject.AddComponent<Image>();
            rect_FillArea.SetParent(rect_view);
            rect_FillArea.localScale = Vector3.one;
            rect_FillArea.localPosition = Vector3.zero;
            rect_FillArea.localRotation = Quaternion.identity;
            rect_FillArea.anchorMax = Vector2.one;
            rect_FillArea.anchorMin = Vector2.zero;
            rect_FillArea.offsetMin = Vector3.zero;
            rect_FillArea.offsetMax = Vector2.zero;



            GameObject go_SlidingArea = new GameObject("SlidingArea");
            RectTransform rect_SlidingArea = go_SlidingArea.GetOrAddComponent<RectTransform>();
            rect_SlidingArea.SetParent(rect_view);
            rect_SlidingArea.localScale = Vector3.one;
            rect_SlidingArea.localPosition = Vector3.zero;
            rect_SlidingArea.localRotation = Quaternion.identity;
            rect_SlidingArea.anchorMax = Vector2.one;
            rect_SlidingArea.anchorMin = Vector2.zero;
            rect_SlidingArea.offsetMin = Vector3.zero;
            rect_SlidingArea.offsetMax = Vector2.zero;


            GameObject go_Handle = new GameObject("Handle");
            RectTransform rect_Handle = go_Handle.GetOrAddComponent<RectTransform>();
            rect_Handle.gameObject.AddComponent<CanvasRenderer>();
            rect_Handle.gameObject.AddComponent<Image>();
            rect_Handle.SetParent(rect_SlidingArea);
            rect_Handle.localScale = Vector3.one;

            UIProgressELement view = go_view.GetOrAddComponent<UIProgressELement>();
            view.targetGraphic = go_Handle.GetComponent<Image>();
            view.handleRect = rect_Handle;
            view.m_FillArea = go_FillArea.GetComponent<Image>();
            view.size = 0;





        }
#endif

    }
}